(function($) {
	$.fn.scheduler = function(options) {
		
		var container = this;
		var currentDate;
		var today = new Date();
		//currently set monday as startWeek
		var startWeek = 1; 
		//enabled date range from start - end
		var start;		   
		var end;
		
		function renderMonth(_currentDate){
			currentDate = _currentDate;
			start = getStartDayOfThisMonth(currentDate);
			end = getEndDayOfThisMonth(currentDate);
			
			//initialize the month array
			var monthArray = new Array(6);
			for(var i=0; i<6; i++){
				monthArray[i] = new Array(7);
			}
			
			//fill date
			var initalStartDate = new Date(start);
			var startIndex = getStartIndex(initalStartDate.getDay());
			monthArray[0][startIndex] = new Date(initalStartDate);
			for(var i=0; i<6; i++){
				for(var j=startIndex; j<7; j++){
					monthArray[i][j] = new Date(initalStartDate);
					initalStartDate.setDate(initalStartDate.getDate() + 1);
				}
				startIndex = 0;
			}
			initalStartDate = new Date(start);
			startIndex = getStartIndex(initalStartDate.getDay());
			if(startIndex != 0){
				for(var i=startIndex; i>=0; i--){
					monthArray[0][i] = new Date(initalStartDate);
					initalStartDate.setDate(initalStartDate.getDate() - 1);
				}
			}
			
			//draw table
			var table = $("<table style='width: 100%' class='scheduler' />").appendTo(container);
			var thead = "<thead><tr>";
			for (var i=0; i<7; i++) {
				thead += "<th class='" + $.scheduler.dayNames[i] + "'>" + $.scheduler.dayNames[i] + "</th>";
			}
			thead = $(thead + "</tr></thead>").appendTo(table);
			var tbody = "<tbody>";
			for(var i=0; i<6; i++){
				tbody += "<tr>";
				for(var j=0; j<7; j++){
					var dateObj = new Date(monthArray[i][j]);
					if(compareCurrentMonth(currentDate, dateObj)){
						if(compareTodayDate(today, dateObj)){
							tbody += "<td class='today'title='" + dateObj.toLocaleDateString() + "'>";
						}else{
							tbody += "<td class='active'title='" + dateObj.toLocaleDateString() + "'>";
						}
						
					}else{
						tbody += "<td class='inactive' title='" + dateObj.toLocaleDateString() + "'>";
					}
					tbody += dateObj.getDate();
					tbody += "</td>";
				}
				tbody += "</tr>";
			}
			tbody = $(tbody + "</tbody>").appendTo(table);
		}
		/**
		*
		*Render main functions
		*
		*
		*/
		renderMonth(today);
		
		return this;
	}
	
	function getStartIndex(day){
		var index = day - 1;
		if(index < 0){
			index = 6;
		}
		return index;
	}
		
	function increaseMonth(currentDate){
		currentDate.setMonth(currentDate.getMonth()+ 1);
		return currentDate;
	}
	
	function decreaseMonth(currentDate){
		currentDate.setMonth(currentDate.getMonth() - 1);
		return currentDate;
	}
	
	function increaseYear(currentDate){
		currentDate.setFullYear(currentDate.getFullYear() + 1);
		return currentDate;
	}
	
	function decreaseYear(currentDate){
		currentDate.setFullYear(currentDate.getFullYear() - 1);
		return currentDate;
	}
	
	function getStartDayOfThisMonth(currentDate){
		var startDay = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
		return startDay;
	}
	
	function getEndDayOfThisMonth(currentDate){
		var endDay = new Date(currentDate.getFullYear(), currentDate.getMonth()+1, 0);
		return endDay;
	}
	
	function compareTodayDate(today, dateObj){
		if(today.getFullYear() == dateObj.getFullYear() 
			&& today.getDate() == dateObj.getDate()
			&& today.getMonth() == dateObj.getMonth()){
			return true;
		}
		return false;
	}
	
	function compareCurrentMonth(currentDate, otherDate){
		if(currentDate.getMonth() == otherDate.getMonth()){
			return true;
		}
		return false;
	}

	$.scheduler = {
		monthNames: ['January','February','March','April','May','June','July','August','September','October','November','December'],
		monthAbbrevs: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
		dayNames: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday', 'Sunday'],
		dayAbbrevs: ['Mon','Tue','Wed','Thu','Fri','Sat', 'Sun']
	}
	
		
})(jQuery);